<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Hash;
use Auth;
use Illuminate\Notifications\Notifiable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use DB;

/**
 * Class Admin
 * @package App
 */
class Admin extends Authenticatable
{
    use Notifiable;
    use EntrustUserTrait;

    
    public $timestamps = false;
    protected $table = 'admin';


    static $StatusDisplay = [
        self::STATUS_ON => '正常',
        self::STATUS_OFF => '禁用',
    ];
    const STATUS_ON = 1;
    const STATUS_OFF = 2;
    
    
    /**
     * 登录
     * @param $name
     * @param $password
     * @return array
     */
    static function Login($name, $password)
    {
        $admin = Admin::where('name', $name)->first();
        if (!$admin) {
            return ['status' => 2, 'message' => '无此用户'];
        }
        if (Hash::check($password, $admin->password)) {
            Auth::login($admin);
            return ['status' => 1, 'message' => '登录成功'];
        }
        return ['status' => 2, 'message' => '密码错误'];
    }


    //

    /**
     * 修改密码
     * @param $datas
     * @return array
     */
    static function PwdEidt($datas)
    {
        $admin = Admin::findOrFail(auth::id());
        $admin->password = bcrypt($datas->newpwd);
        if ($admin->save()) {
            return ['status' => 1, 'message' => '修改成功'];
        } else {
            return ['status' => 1, 'message' => '修改失败'];
        }
    }


    /**
     * 创建管理员
     * @param $datas
     * @return array
     */
    static function CreateAdmin($datas)
    {
        $admin = new Admin();
        $admin->name = $datas->name;
        $admin->remember_token = $datas->_token;
        $admin->password = bcrypt(request('password'));

        $admin->created_at = $admin->updated_at = time();
        if($admin->save()) {
            return ['status' => 1, 'message'=> '添加成功'];
        } else {
            return ['status' => 1, 'message'=> '添加失败'];
        }
    }

    /**
     * 修改管理员
     * @param $adminid
     * @return array
     */
    static function ModifyAdmin($adminid)
    {
        $admin = Admin::findOrFail($adminid);
        $admin->updated_at = time();
        // 姓名
        if (request()->has('name')) {
            $admin->name = request('name');
        }
        // 密码
        if (request()->has('password') && request()->password) {
            $admin->password = bcrypt(request('password'));
        }
        if (request()->has('roles')) {
            //先把之前拥有的角色都清除掉
            DB::table('role_user')->where('user_id', $adminid)->delete();
            // 设置角色\
            if ($roles = request('roles', null)) {
                foreach ($roles as $role) {
                    $admin->roles()->attach($role);
                }
            }
        } else {
            DB::table('role_user')->where('user_id', $adminid)->delete();
        }
        if($admin->save()) {
            return ['status' => 1, 'message'=> '修改成功'];
        } else {
            return ['status' => 1, 'message'=> '失败'];
        }
    }
}

